// 封装网络基础请求
import axios from "axios";
import {ElMessage} from "element-plus";

const defaultError = ()=>ElMessage.error('发生未知错误，请联系管理员')
const defaultFailure = (message)=>ElMessage.warning(message)

function post(url, data, success, failure = defaultFailure, error = defaultError){
    axios.post(url, data, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        // cookie 请求
        withCredentials: true
    }).then(({data}) => {
        console.log(data)
        if(data.success){
            success(data.message, data.status)
        }
        else{
            failure(data.message, data.status)
        }
    }).catch(error)
}

function get(url, success, failure = defaultFailure, error = defaultError){
    axios.post(url,  {
        // cookie 请求
        withCredentials: true
    }).then(({data}) => {
        if(data.success){
            success(data.message, data.status, data.data)
        }
        else{
            failure(data.message, data.status)
        }
    }).catch(error)
}

// 暴露封装后的函数头
export {get, post}
